﻿<!--#include file="base.asp"-->
<!--#include file="../lib/class/sdcms.upload.asp"-->
<!--#include file="../lib/class/sdcms.draw.asp"-->
<%
''' SDCMS 上传
''' ==================================================================
''' 版权所有 http://www.sdcms.cn
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 编写: IT平民
''' 修改：IT平民 in 2014.07

	sub getjson(byval t0,byval t1,byval t2,byval t3)
		if t0=0 Then
			sdcms.echo "{""url"":"""",""original"":"""",""title"":"""",""state"":"""&t3&"""}"
		else
			sdcms.echo "{""url"":"""&t1&""",""original"":"""&t2&""",""title"":"""&t2&""",""state"":"""&t3&"""}"
		end if
	end sub
	
	sub setjson(byval t0,byval t1)
		if t0=0 Then
			sdcms.echo "{""status"":""n"",""info"":"""&t1&"""}"
		else
			sdcms.echo "{""status"":""y"",""info"":"""&t1&"""}"
		end if
	end sub
	
	function get_uploaderr(byval t0)
		dim t1:t1=""
		select case t0
			case -1:t1="没有文件上传"
			case 1:t1="文件过大，上传失败"
			case 2:t1="不允许被上传的文件"
			case 3:t1="文件过大，且不允许被上传"
			case 4:t1="文件保存失败"
			case 5:t1="禁止上传此类文件"
		end select
		get_uploaderr=t1
	end function
	
	sub errdb()
		sdcms.echo "&#38169;&#35823;&#30340;&#21442;&#25968;"
	end sub
	
	sub adddb(byval t0,byval t1,byval t2,byval t3,byval t4,byval t5,byval t6,byval t7,byval t8,byval t9)
		sdcms.db.insert "sd_attachment",array(array("classid",t9,0,0),array("cid",t8,0,0),array("filepath",t0,255,1),array("filename",t1,255,1),array("filesize",sdcms.getnum(t2),255,1),array("oldname",t3,255,1),array("fileext",t4,50,1),array("ispic",sdcms.is_pic("."&t4),1,0),array("createip",sdcms.getip,255,1),array("createdate",t5,255,0),array("adminid",t6,0,0),array("userid",t7,0,0),array("fullname",t0&t1,0,1))
		if sdcms.is_pic(t1)=1 then
			dim draw
			set draw=new sdcms_draw
				draw.add t0&t1
			set draw=nothing
		end if
	end sub
	
	function getfile_ext(t0)
		dim t1
		select case lcase(t0)
			case "jpg","gif","png":t1="pic"
			case "docx":t1="doc"
			case "xlsx":t1="xls"
			case "pptx":t1="ppt"
			case "rar","zip","7z":t1="rar"
			case  "ppt","xls","doc","pdf","txt","flv","swf":t1=t0
			case else:t1="blank"
		end select
		getfile_ext=t1
	end function
	
	sub editordb()
		if isuplogin=0 then
			getjson 0,"","","登录失败，请退出重新登陆_editor"
			exit sub
		end if
		dim upload,filetype,uptype,classid,cid,pictitle
		filetype=sdcms.fget("filetype",0)
		classid=sdcms.getint(sdcms.fget("classid",0),0)
		cid=sdcms.getint(sdcms.fget("cid",0),0)
		select case filetype
			case "2":uptype=sdcms.getsys("uptype_pic")&"|"&sdcms.getsys("uptype_video")&"|"&sdcms.getsys("uptype_file")
			case else:uptype=sdcms.getsys("uptype_pic")
		end select
		
		set upload=new sdcms_upload
			upload.filetype=uptype
			upload.savepath=sdcms.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=sdcms.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			dim admin_id:admin_id=sdcms.getint(sdcms.fget("admin_id",0),0)
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				pictitle=upload.form("pictitle")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if pictitle="" then pictitle=oldname
				if sdcms.strlen(errstr)>0 then
					getjson 0,"","",errstr
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,admin_id,0,cid,classid
					getjson 1,filename,pictitle,"SUCCESS"
				end if
			next
		set upload=nothing
	end sub
	
	sub swfdb()
		if isuplogin=0 then
			sdcms.echo "0,登录失败，请退出重新登陆_swf"
			exit sub
		end if
		dim upload,filetype,uptype,classid,cid
		filetype=sdcms.fget("filetype",0)
		classid=sdcms.getint(sdcms.fget("classid",0),0)
		cid=sdcms.getint(sdcms.fget("cid",0),0)
		select case filetype
			case "2":uptype=sdcms.getsys("uptype_file")
			case "3":uptype=sdcms.getsys("uptype_video")
			case "4":uptype=sdcms.getsys("uptype_pic")&"|"&sdcms.getsys("uptype_video")&"|"&sdcms.getsys("uptype_file")
			case "5":uptype="swf"
			case else:uptype=sdcms.getsys("uptype_pic")
		end select
		set upload=new sdcms_upload
			upload.filetype=uptype
			upload.savepath=sdcms.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=sdcms.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			dim admin_id:admin_id=sdcms.getint(sdcms.fget("admin_id",0),0)
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if sdcms.strlen(errstr)>0 then
					sdcms.echo "0,"&errstr&","","
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,admin_id,0,cid,classid
					dim thisid
					dim rs:set rs=sdcms.db.exedb("select top 1 id from sd_attachment where filepath='"&filepath&"' and filename='"&filenew&"'")
					if rs.eof then
						thisid=0
					else
						thisid=rs(0)
					end if
					rs.close
					set rs=nothing
					sdcms.echo ""&thisid&","&filename&","&getfile_ext(fileext)&","&oldname&""
				end if
			next
		set upload=nothing
	end sub
	
	sub jsondb(byval t0)
		if isuplogin=0 then
			getjson 0,"","","登录失败，请退出重新登陆_json"
			exit sub
		end if
		dim upload,classid,cid
		classid=sdcms.getint(sdcms.fget("classid",0),0)
		cid=sdcms.getint(sdcms.fget("cid",0),0)
		
		set upload=new sdcms_upload
			upload.filetype=t0
			upload.savepath=sdcms.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=sdcms.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			dim admin_id:admin_id=sdcms.getint(sdcms.fget("admin_id",0),0)
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if sdcms.strlen(errstr)>0 then
					setjson 0,"",errstr
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,admin_id,0,cid,classid
					setjson 1,filename
				end if
			next
		set upload=nothing
	end sub
	
	sub configdb()
		if isuplogin=0 then exit sub
		Response.AddHeader "Content-Type", "text/plain"
		dim html:html=""
		html=html&"{""imageActionName"": ""editor"","
		html=html&"""imageFieldName"": ""upfile"","
		html=html&"""imageMaxSize"": "&sdcms.getsys("upmaxsize")*1000&","
		html=html&"""imageAllowFiles"": ["".jpg"","".gif""],"
		html=html&"""imageCompressEnable"": false,"
		html=html&"""imageCompressBorder"": 1600,"
		html=html&"""imageInsertAlign"": ""none"","
		html=html&"""imageUrlPrefix"": """","
		html=html&"""imagePathFormat"": """","
		html=html&"""videoActionName"": ""editor"","
		html=html&"""videoFieldName"": ""upfile"","
		html=html&"""videoPathFormat"": """","
		html=html&"""videoUrlPrefix"": """","
		html=html&"""videoMaxSize"": "&sdcms.getsys("upmaxsize")*1000&","
		html=html&"""videoAllowFiles"": ["
		html=html&"    "".flv"", "".swf"", "".mkv"", "".avi"", "".rm"", "".rmvb"", "".mpeg"", "".mpg"","
		html=html&"    "".ogg"", "".ogv"", "".mov"", "".wmv"", "".mp4"", "".webm"", "".mp3"", "".wav"", "".mid""],"
		html=html&"""fileActionName"": ""editor"","
		html=html&"""fileFieldName"": ""upfile"","
		html=html&"""filePathFormat"": """","
		html=html&"""fileUrlPrefix"": """","
		html=html&"""fileMaxSize"": "&sdcms.getsys("upmaxsize")*1000&","
		html=html&"""fileAllowFiles"": ["
		html=html&"    "".png"", "".jpg"", "".jpeg"", "".gif"", "".bmp"","
		html=html&"    "".flv"", "".swf"", "".mkv"", "".avi"", "".rm"", "".rmvb"", "".mpeg"", "".mpg"","
		html=html&"    "".ogg"", "".ogv"", "".mov"", "".wmv"", "".mp4"", "".webm"", "".mp3"", "".wav"", "".mid"","
		html=html&"    "".rar"", "".zip"", "".tar"", "".gz"", "".7z"", "".bz2"", "".cab"", "".iso"","
		html=html&"    "".doc"", "".docx"", "".xls"", "".xlsx"", "".ppt"", "".pptx"", "".pdf"", "".txt"", "".md"", "".xml"""
		html=html&"]"
		html=html&"}"
		sdcms.echo html
	end sub
	
	on error resume next
	dim act:act=sdcms.fget("act",0)
	select case act
		case "editor":editordb
		case "swf":swfdb
		case "json":jsondb(sdcms.getsys("uptype_pic"))
		case "json_down":jsondb(sdcms.getsys("uptype_file"))
		case "other":islogin:errdb
		case "config":configdb
	end select
%>